﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;
using HYFamilyLibDataManager;

namespace YArchitech.DataModel
{
	public class FamilyDataReader
	{
		public FamilyDataReader(SubjectType subjectType, Autodesk.Revit.DB.Document doc)
		{
			this.db = FamilyLibDatabase.GetSqliteDB(subjectType, RevitVersion.GetVersionNumber(doc));
		}

		public List<string> GetFamilySort(string categoryName)
		{
			List<DbCategory> allChildren = this.db.CategoryTable[categoryName].GetAllChildren(false);
			List<string> list = new List<string>();
			foreach (DbCategory dbCategory in allChildren)
			{
				list.Add(dbCategory.Name);
			}
			return list;
		}

		public List<string> GetFamilySort(string categoryName, string parentName)
		{
			List<DbCategory> allChildren = this.db.CategoryTable[parentName][categoryName].GetAllChildren(false);
			List<string> list = new List<string>();
			foreach (DbCategory dbCategory in allChildren)
			{
				list.Add(dbCategory.Name);
			}
			return list;
		}

		public TreeView GetFamilyAllInfoTree(string categoryName)
		{
			List<DbCategory> allChildren = this.db.CategoryTable[categoryName].GetAllChildren(false);
			List<DbFamily> allFamilies = this.db.CategoryTable[categoryName].GetAllFamilies(false, CheckType.All);
			return this.GetFamilyAllInfoTree(allChildren, allFamilies);
		}

		public List<DbFamily> GetFamilyAllInfoList(string categoryName)
		{
			List<DbFamily> list = new List<DbFamily>();
			foreach (DbCategory dbCategory in this.db.CategoryTable[categoryName].GetAllChildren(false))
			{
				List<DbFamily> allInfoList = this.GetAllInfoList(dbCategory);
				if (allInfoList != null)
				{
					list.AddRange(allInfoList);
				}
			}
			List<DbFamily> allFamilies = this.db.CategoryTable[categoryName].GetAllFamilies(false, CheckType.All);
			if (allFamilies != null)
			{
				list.AddRange(allFamilies);
			}
			return list;
		}

		private List<DbFamily> GetAllInfoList(DbCategory dbCategory)
		{
			List<DbFamily> list = new List<DbFamily>();
			if (dbCategory != null)
			{
				foreach (DbCategory dbCategory2 in dbCategory.GetAllChildren(false))
				{
					List<DbFamily> allInfoList = this.GetAllInfoList(dbCategory2);
					if (allInfoList != null)
					{
						list.AddRange(allInfoList);
					}
				}
				this.GetAllFamilyList(dbCategory, ref list);
			}
			return list;
		}

		private void GetAllFamilyList(DbCategory dbCategory, ref List<DbFamily> allFamily)
		{
			if (dbCategory != null)
			{
				List<DbFamily> allFamilies = dbCategory.GetAllFamilies(false, CheckType.All);
				allFamily.AddRange(allFamilies);
			}
		}

		public List<DBFamilyListItem> GetFamilyList(string categoryName)
		{
			List<DBFamilyListItem> list = new List<DBFamilyListItem>();
			foreach (DbFamily dbFamily in this.db.CategoryTable[categoryName].GetAllFamilies(false, CheckType.All))
			{
				DBFamilyListItem item = new DBFamilyListItem(dbFamily.Name, dbFamily.KeyName, dbFamily);
				list.Add(item);
			}
			return list;
		}

		public DBFamilyListItem[] GetFamilyArray(string categoryName)
		{
			List<DBFamilyListItem> familyList = this.GetFamilyList(categoryName);
			DBFamilyListItem[] array = new DBFamilyListItem[familyList.Count];
			for (int i = 0; i < familyList.Count; i++)
			{
				array[i] = familyList[i];
			}
			return array;
		}

		public TreeView GetFamilyAllInfoTree(string categoryName, string parentName)
		{
			List<DbCategory> allChildren = this.db.CategoryTable[parentName][categoryName].GetAllChildren(false);
			List<DbFamily> allFamilies = this.db.CategoryTable[parentName][categoryName].GetAllFamilies(false, CheckType.All);
			return this.GetFamilyAllInfoTree(allChildren, allFamilies);
		}

		public TreeNode GetFamilyAllInfoTree(string categoryName, string parentName, bool isReadFamily)
		{
			List<DbCategory> allChildren = this.db.CategoryTable[parentName][categoryName].GetAllChildren(false);
			TreeNode result = new TreeNode();
			foreach (DbCategory dbCategory in allChildren)
			{
				result = this.GetAllInfo(dbCategory);
			}
			return result;
		}

		public TreeView GetFamilyAllInfoTree(string categoryName, string parentName, string pParentName)
		{
			List<DbCategory> allChildren = this.db.CategoryTable[pParentName][parentName][categoryName].GetAllChildren(false);
			List<DbFamily> allFamilies = this.db.CategoryTable[pParentName][parentName][categoryName].GetAllFamilies(false, CheckType.All);
			return this.GetFamilyAllInfoTree(allChildren, allFamilies);
		}

		public void GetDuctFittingSetFamilyAllInfoTree(string categoryName, string parentName, TreeView newtreeview)
		{
			List<DbCategory> allChildren = this.db.CategoryTable[parentName][categoryName].GetAllChildren(false);
			TreeNode treeNode = new TreeNode(categoryName);
			newtreeview.Nodes.Add(treeNode);
			this.addNode(treeNode, allChildren);
		}

		public List<DbFamily> GetDuctFittingSetFamilyAllInfoTree(string categoryName, string parentName, string pParentName)
		{
			this.db.CategoryTable[pParentName][parentName][categoryName].GetAllChildren(false);
			return this.db.CategoryTable[pParentName][parentName][categoryName].GetAllFamilies(false, CheckType.All);
		}

		private void addNode(TreeNode tv, List<DbCategory> category)
		{
			foreach (DbCategory dbCategory in category)
			{
				TreeNode allInfo = this.GetAllInfo(dbCategory);
				if (allInfo != null)
				{
					tv.Nodes.Add(allInfo);
				}
			}
		}

		public TreeView GetPipeAccessoryFamilyAllInfoTree(string categoryName, string categoryName2, string categoryName3)
		{
			List<DbCategory> allChildren = this.db.CategoryTable[categoryName].GetAllChildren(false);
			List<DbFamily> allFamilies = this.db.CategoryTable[categoryName].GetAllFamilies(false, CheckType.All);
			List<DbCategory> allChildren2 = this.db.CategoryTable[categoryName2].GetAllChildren(false);
			List<DbFamily> allFamilies2 = this.db.CategoryTable[categoryName2].GetAllFamilies(false, CheckType.All);
			List<DbCategory> allChildren3 = this.db.CategoryTable[categoryName3].GetAllChildren(false);
			List<DbFamily> allFamilies3 = this.db.CategoryTable[categoryName3].GetAllFamilies(false, CheckType.All);
			TreeView treeView = new TreeView();
			TreeNode treeNode = new TreeNode(categoryName);
			TreeNode treeNode2 = new TreeNode(categoryName2);
			TreeNode treeNode3 = new TreeNode(categoryName3);
			treeView.Nodes.Add(treeNode);
			treeView.Nodes.Add(treeNode2);
			treeView.Nodes.Add(treeNode3);
			this.addNode(treeNode, allChildren, allFamilies);
			this.addNode(treeNode2, allChildren2, allFamilies2);
			this.addNode(treeNode3, allChildren3, allFamilies3);
			return treeView;
		}

		private void addNode(TreeNode tv, List<DbCategory> category, List<DbFamily> dbFamily)
		{
			List<DbCategory> list = new List<DbCategory>();
			List<DbFamily> list2 = new List<DbFamily>();
			List<string> list3 = new List<string>();
			List<string> list4 = new List<string>();
			foreach (DbCategory dbCategory in category)
			{
				list3.Add(dbCategory.Name);
			}
			foreach (DbFamily dbFamily2 in dbFamily)
			{
				list4.Add(dbFamily2.Name);
			}
			list3.Sort();
			list4.Sort();
			foreach (string b in list3)
			{
				foreach (DbCategory dbCategory2 in category)
				{
					if (dbCategory2.Name == b)
					{
						list.Add(dbCategory2);
					}
				}
			}
			foreach (string b2 in list4)
			{
				foreach (DbFamily dbFamily3 in dbFamily)
				{
					if (dbFamily3.Name == b2)
					{
						list2.Add(dbFamily3);
					}
				}
			}
			foreach (DbCategory dbCategory3 in list)
			{
				TreeNode allInfo = this.GetAllInfo(dbCategory3);
				if (allInfo != null)
				{
					tv.Nodes.Add(allInfo);
				}
			}
			foreach (DbFamily dbFamily4 in list2)
			{
				TreeNode allSymbol = this.GetAllSymbol(dbFamily4);
				if (allSymbol != null)
				{
					tv.Nodes.Add(allSymbol);
				}
			}
		}

		private TreeView GetFamilyAllInfoTree(List<DbCategory> category, List<DbFamily> dbFamily)
		{
			TreeView treeView = new TreeView();
			foreach (DbCategory dbCategory in category)
			{
				TreeNode allInfo = this.GetAllInfo(dbCategory);
				if (allInfo != null)
				{
					treeView.Nodes.Add(allInfo);
				}
			}
			foreach (DbFamily dbFamily2 in dbFamily)
			{
				TreeNode allSymbol = this.GetAllSymbol(dbFamily2);
				if (allSymbol != null)
				{
					treeView.Nodes.Add(allSymbol);
				}
			}
			return treeView;
		}

		private TreeNode GetAllInfo(DbCategory dbCategory)
		{
			TreeNode treeNode = null;
			if (dbCategory != null)
			{
				treeNode = new TreeNode(dbCategory.Name);
				treeNode.Name = dbCategory.Name;
				treeNode.Tag = dbCategory;
				treeNode.ToolTipText = dbCategory.Name;
				foreach (DbCategory dbCategory2 in dbCategory.GetAllChildren(false))
				{
					TreeNode allInfo = this.GetAllInfo(dbCategory2);
					if (allInfo != null)
					{
						treeNode.Nodes.Add(allInfo);
					}
				}
				this.GetAllFamily(dbCategory, ref treeNode);
			}
			return treeNode;
		}

		private void GetAllFamily(DbCategory dbCategory, ref TreeNode tn)
		{
			if (dbCategory != null)
			{
				foreach (DbFamily dbFamily in dbCategory.GetAllFamilies(false, CheckType.All))
				{
					TreeNode allSymbol = this.GetAllSymbol(dbFamily);
					if (allSymbol != null)
					{
						tn.Nodes.Add(allSymbol);
					}
				}
			}
		}

		private TreeNode GetAllSymbol(DbFamily dbFamily)
		{
			TreeNode treeNode = null;
			if (dbFamily != null)
			{
				treeNode = new TreeNode(dbFamily.Name);
				treeNode.Name = dbFamily.Name;
				treeNode.Tag = dbFamily;
				treeNode.ToolTipText = dbFamily.Name;
			}
			return treeNode;
		}

		public string GetFirstSymbolName(DbFamily dbFamily)
		{
			return this.GetFirstFamilySymbol(dbFamily).Name;
		}

		public List<DbParameter> GetSymbolParameter(DbFamily family, string typeName)
		{
			DbFamilySymbol dbFamilySymbol = this.db.TypeTable[family, typeName];
			List<DbParameter> list = new List<DbParameter>();
			if (dbFamilySymbol != null)
			{
				foreach (DbParameter item in dbFamilySymbol.Params)
				{
					list.Add(item);
				}
			}
			return list;
		}

		public DbFamilySymbol GetFamilySymbol(DbFamily family, string typeName)
		{
			return this.db.TypeTable[family, typeName];
		}

		public DbFamilySymbol GetFirstFamilySymbol(DbFamily family)
		{
			DbFamilySymbol result = null;
			foreach (string text in family.Symbols)
			{
				if (!string.IsNullOrEmpty(text))
				{
					result = this.db.TypeTable[family, text];
					break;
				}
			}
			return result;
		}

		public DataTable GetFamilyParams(DbFamily family)
		{
			return this.db.TypeTable.DataTable(family);
		}

		public FamilySymbol FamilyLoad(DbFamily family, string typeName, Autodesk.Revit.DB.Document doc)
		{
			DbFamilySymbol familySymbol = this.GetFamilySymbol(family, typeName);
			FamilySymbol result;
			try
			{
				result = familySymbol.Create(doc);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FamilySymbol FamilyLoad(DbFamily family, Autodesk.Revit.DB.Document doc)
		{
			DbFamilySymbol familySymbol = this.GetFamilySymbol(family, this.GetFirstSymbolName(family));
			FamilySymbol result;
			try
			{
				result = familySymbol.Create(doc);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public DbFamily GetFamilyByName(string familyName)
		{
			return this.db.FamilyTable[familyName];
		}

		public DbFamilySymbol GetFamilyFirstTypeByName(string familyName)
		{
			DbFamily family = this.db.FamilyTable[familyName];
			return this.GetFirstFamilySymbol(family);
		}

		private FamilyLibDatabase db;
	}
}
